//-----------------Sonic CD BGEffects Script------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
// Values 0-1 are unused...
private alias object.value2 : Object.PrevAngle
private alias object.value3 : Object.FansTimer
private alias object.value4 : Object.CrunchersTimer
private alias object.value5 : Object.FansSprite
private alias object.value6 : Object.CrunchersSprite

// Priority
private alias 1 : PRIORITY_ACTIVE

// Deformation Layer
private alias 2 : DEFORM_BG


event ObjectDraw
	// Animate the Fans
	Object.FansTimer++
	if Object.FansTimer > 2
		Object.FansTimer = 0
		Object.FansSprite++
		Object.FansSprite %= 3
		switch Object.FansSprite
		case 0
			// Fans are 4x4 tiles, with 8 unique tiles in them, so
			// 8 tiles need to be updated to change the entire sprite
			Copy16x16Tile(260, 420)
			Copy16x16Tile(259, 421)
			Copy16x16Tile(258, 422)
			Copy16x16Tile(257, 423)
			Copy16x16Tile(264, 424)
			Copy16x16Tile(263, 425)
			Copy16x16Tile(262, 426)
			Copy16x16Tile(261, 427)
			break

		case 1
			Copy16x16Tile(260, 428)
			Copy16x16Tile(259, 429)
			Copy16x16Tile(258, 430)
			Copy16x16Tile(257, 431)
			Copy16x16Tile(264, 432)
			Copy16x16Tile(263, 433)
			Copy16x16Tile(262, 434)
			Copy16x16Tile(261, 435)
			break

		case 2
			Copy16x16Tile(260, 436)
			Copy16x16Tile(259, 437)
			Copy16x16Tile(258, 438)
			Copy16x16Tile(257, 439)
			Copy16x16Tile(264, 440)
			Copy16x16Tile(263, 441)
			Copy16x16Tile(262, 442)
			Copy16x16Tile(261, 443)
			break

		end switch
	end if

	// Animate the Crunchers
	Object.CrunchersTimer++
	if Object.CrunchersTimer > 5
		Object.CrunchersTimer = 0
		Object.CrunchersSprite++
		Object.CrunchersSprite &= 3
		switch Object.CrunchersSprite
		case 0
			// Crunchers are 2x2 tiles, so 4 tiles need to be updated
			// in order to completely change their sprite
			Copy16x16Tile(99, 444)
			Copy16x16Tile(98, 445)
			Copy16x16Tile(101, 448)
			Copy16x16Tile(100, 449)
			break

		case 1
			Copy16x16Tile(99, 446)
			Copy16x16Tile(98, 447)
			Copy16x16Tile(101, 450)
			Copy16x16Tile(100, 451)
			break

		case 2
			Copy16x16Tile(99, 452)
			Copy16x16Tile(98, 453)
			Copy16x16Tile(101, 456)
			Copy16x16Tile(100, 457)
			break

		case 3
			Copy16x16Tile(99, 454)
			Copy16x16Tile(98, 455)
			Copy16x16Tile(101, 458)
			Copy16x16Tile(100, 459)
			break

		end switch
	end if

	// Find the difference between the current scroll and last scroll pos
	temp1 = Object.PrevAngle
	temp2 = tileLayer[0].angle
	if temp1 < temp2
		temp3 = temp2
		temp3 -= temp1
		if temp3 > 256
			// Account for looping
			temp1 += 512
		end if
	else
		temp3 = temp1
		temp3 -= temp2
		if temp3 > 256
			// Account for looping (but now the other way!)
			temp2 += 512
		end if
	end if

	temp0 = temp1
	temp0 -= temp2

	// Store the current angle for use next frame
	Object.PrevAngle = tileLayer[0].angle

	// Move all the other BG layers in accordance with the difference from last frame

	temp1 = 0x02000 // This value isn't used, though it corresponds to the first unused background layer
	temp2 = 0x20000
	temp3 = 0x10000

	temp1 *= temp0 // See above
	temp2 *= temp0
	temp3 *= temp0

	// Presumably there was one more parallax updating here before that got cut, my best guess is that it'd be something like
	//	HParallax[1].ScrollPos += TempValue1
	// That would update the background water in the scene, provided the layer is left enabled
	// HParallax[0] is an option too, that would be the unused wave BG layer (disabled too)

	hParallax[2].scrollPos += temp2
	hParallax[3].scrollPos += temp3

end event


event ObjectStartup

	// Setup stage bounds
	stage.curXBoundary1 = 0x3800000 // 896 pixels to the left
	stage.curXBoundary2 = 0xC800000 // 3200 pixels to the right
	stage.curYBoundary1 = 0x3800000 // 896 pixels upwards
	stage.curYBoundary2 = 0xC800000 // 3200 pixels downwards

	// Set the initial BG position

	// Move the first layer 15 pixels to the right
	// -> I don't believe this does anything, though, as it's shifting the unused BG layer which later gets disabled anyways...
	hParallax[0].scrollPos += 0xF0000

#platform: HW_Rendering
	// Load the stage map sprite if using the hardware render

	LoadSpriteSheet("Special/SSMap1.gif")
#endplatform

	// Setup all BGEffect objects present in the level
	arrayPos0 = 32
	while arrayPos0 < 1056
		if object[arrayPos0].type == TypeName[BGEffects]

			// Set this object to always active
			object[arrayPos0].priority = PRIORITY_ACTIVE

			// Make it be among the first things to be processed for drawing
			object[arrayPos0].drawOrder = 0

			// Starting angle, assuming that Sonic is starting facing left
			Object.PrevAngle[arrayPos0] = 368

		end if

		arrayPos0++
	loop

	SetLayerDeformation(DEFORM_BG, 128, 16, 0, 0, 0)

end event


// ========================
// Editor Subs
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Special/SSMap1.gif")
	SpriteFrame(-256, -256, 512, 512, 0, 0)


end event
